/**
 * @author 小泽玛丽罗
 */
var tmpl_head='<div id="${nickname}" class="ub" onclick="chat(this,${uid})" style="min-height:4em;border-bottom: 1px solid rgb(105, 101, 101);">'
			          +'<div class=" umar-a ub-f1"><div class="uabs umw3 umh5" style="background-image:url(${avatar_url})"></div></div>'
					  +'<div class=" ub-f1 min-h4  fontc text-c wrap">${nickname}</div>'
					  +'<div class=" ub-f1 imgArrowR" ontouchstart="zy_touch()" onclick=""></div>'
				 +'</div>';
var tmpl_add_Fol='<div id="Friend" name="Friend" onclick="selectFriend(this,true)" class="ub umh5  unn c-wh tx-c ub-ver" style="border:1px solid #c5c5c5;background-color:#ebebeb; ">'
		+'<div ontouchstart="zy_touch('+'btn-act'+')" class="btn  uinn5 uc-a t-bla">更多</div>'
 +'</div>';
 var tmpl_add_Rec='<div id="Friend" name="Friend" onclick="pullRecentContact()" class="ub umh5  unn c-wh tx-c ub-ver" style="border:1px solid #c5c5c5;background-color:#ebebeb; ">'
		+'<div ontouchstart="zy_touch('+'btn-act'+')" class="btn  uinn5 uc-a t-bla">更多</div>'
 +'</div>';
var tmpl_left='<div class="ub ub-ver">'
		+'<div class="ver-c min-h2 input-c2 ub  "> '
			+'<div class="tx-c ub-f1 "><label class="max-w3 min-h2 t-wh " style="background-color:#cccccc;"> ${ctime}</label></div>'
		+'</div>'
		+'<div class=" ub min-h3">'
			+'<div class="ub-f1 max-w4 ver-c " > <img class="  radiusH min-h3" src="${userHead}"></div>'
			+'<div class="umw4 max-w13 ">'
				+'<div class="bubbleL ulev1  ver-cc"> <label style="margin:1.3em;line-height:1.3em;word-break:break-all; ">'
					+'${content}'
				+'</label></div>'
			+'</div>'
		+'</div>'
	+'</div>';
	var tmpl_right='<div class="ub ub-ver">'
		+'<div class="ver-c min-h2 input-c2 ub  "> '
			+'<div class="tx-c ub-f1 "><label class="max-w3 min-h2 t-wh " style="background-color:#cccccc;"> ${ctime}</label></div>'
		+'</div>'

		+'<div class=" ub min-h3 tx-r">'
			+'<div class="ub-f1"></div>'
			+'<div class="umw4 max-w13">'
				+'<div class="bubbleR ulev1 max-w13 ver-cc" style="padding:0.2em 0.5em 0em 0.3em;"> <label style="margin:1em;line-height:1.3em;word-break:break-all; ">'
					+'${content}'
				+'</label></div>'
			+'</div>'
			+'<div class="ub-f1  max-w4 " > <img class="  radiusH min-h3" src="${userHead}"></div>'
		+'</div>'
	+'</div>';
 
 var tmpl_recentContact='<div class="ub max-h5" style="border-bottom:1px solid black;min-height:3.5em;">'
							+'<div id="${uid}" name="${last_message_id}" value="${nickname}" class="ub-f1 max-h4 max-w4 umar-a" onclick="enterChat(this)"><img style="width:100%;" src="css/img/headBig.png"></div>'
							+'<div id="${uid}" name="${last_message_id}" value="${nickname}" class="ub-f4 ub-ver umar-a" onclick="enterChat(this)" >'
								+'<div class="ub-f3 ub-ver" >'
									+'<div class="ub ub-f1" style="font-weight:bold;">${nickname}</div>'
									+'<div class="ub ub-f1 min-h2 ulev ver-c">'
										+'${ctime}:${content}'
									+'</div>'
								+'</div>'
							+'</div>'
							
						+'</div>';
 
var sendUrl='http://www.diaoyumi.ourstu.com/api/chat/sendMessage';
var friendUrl='http://www.diaoyumi.ourstu.com/api/profile/getFollowingList';
var pullUrl='http://www.diaoyumi.ourstu.com/api/chat/pullMessage';
var pullRecentContactUrl='http://www.diaoyumi.ourstu.com/api/chat/listRecent';
var pullPrevMessageUrl='http://www.diaoyumi.ourstu.com/api/chat/pullPrevMessage';

window.localStorage.setItem("friendOffset",0);

//window.setInterval(pullCahts,750);
/**
 * @argument 用于在聊天室实时拉取消息，并调用浮动窗口的pullDataAppend()函数拼接消息
 */
function pullCahts(){
	var httpID=38756;
	window.localStorage.setItem("is_history",false);
	uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				var count=parseInt(zy_tmpl_count(json["list"]));
				if(count!=0){
					var last_message=json["list"][count-1];
					var last_message_id=last_message["message_id"];
					window.localStorage.setItem("pullData",JSON.stringify(json));
					window.localStorage.setItem("last_message_id",last_message_id);
					uexWindow.evaluatePopoverScript("privateLetter","content","pullDataAppend()")
				}
			}else{
				uexWindow.toast(1,5,json["message"],1000);
			}
		}
	}
	uexXmlHttpMgr.open(httpID,'post',pullUrl,3000);
	uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"uid",window.localStorage["chatUid"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"last_message_id",window.localStorage["last_message_id"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"count",100);
	uexXmlHttpMgr.send(httpID);
}
/**
 * @argument 用于被主窗口调用拼接消息
 */

function pullDataAppend(){
	var json=window.localStorage["pullData"];
	json=eval("("+json+")");
	var count=zy_tmpl_count(json["list"]);
	if(window.localStorage["is_history"]=="false"){
			for (var i = 0; i <count; i++) {
				var obj = json["list"][i];
				obj["ctime"] = addFriendlyTime2(obj.ctime);
				obj["userHead"] = obj["user"].avatar_url;
				var div = document.createElement("DIV");
				if (parseInt(obj["user"].uid) != 1) {
					var message = zy_tmpl_s(tmpl_left,obj,zy_tmpl_count(obj));
					div.innerHTML = message;
					document.body.appendChild(div);
					window.scrollTo(1000000,1000000);
				}
				else {
					var message = zy_tmpl_s(tmpl_right,obj,zy_tmpl_count(obj));
					div.innerHTML = message;
					document.body.appendChild(div);
					window.scrollTo(1000000,1000000);
				}
			}
			
	}else{
		for (var i = count-1; i >=0; i--) {
				var obj = json["list"][i];
				obj["ctime"] = addFriendlyTime2(obj.ctime);
				obj["userHead"] = obj["user"].avatar_url;
				var div = document.createElement("DIV");
				if (parseInt(obj["user"].uid) != 1) {
					var message = zy_tmpl_s(tmpl_left,obj,zy_tmpl_count(obj));
					div.innerHTML = message;
					document.body.appendChild(div);
					window.scrollTo(1000000,1000000);
				}
				else {
					var message = zy_tmpl_s(tmpl_right,obj,zy_tmpl_count(obj));
					div.innerHTML = message;
					document.body.appendChild(div);
					window.scrollTo(1000000,1000000);
				}
		}
		var last_message_data=eval("("+window.localStorage["recentContactData"]+")");
		var last_message_id=window.localStorage["last_message_id"];
		var count=zy_tmpl_count(last_message_data["list"]);
		for(var j=0;j<count;j++){
			var obj=last_message_data["list"][j];
			if(last_message_id==obj["last_message_id"]){
				obj["ctime"]=addFriendlyTime2(obj["last_contact_time"]);
				obj["userHead"]=obj["user"].avatar_url;
				var div = document.createElement("DIV");
				if(obj["user"].uid==window.localStorage["chatUid"]){
					if (parseInt(obj["user"].uid) != 1) {
						var message = zy_tmpl_s(tmpl_left,obj,zy_tmpl_count(obj));
						div.innerHTML = message;
						document.body.appendChild(div);
						window.scrollTo(1000000,1000000);
					}
					else {
						var message = zy_tmpl_s(tmpl_right,obj,zy_tmpl_count(obj));
						div.innerHTML = message;
						document.body.appendChild(div);
						window.scrollTo(1000000,1000000);
					}
				}
			}
		}
		
	}
	
}
/**
 * @argument 用于向对方发送消息，在last_message_id=0是直接拼接消息
 */
function sendMessage(){
	var content=$('#txt').val();
	var httpID=564;
	uexXmlHttpMgr.onData=function (inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				if(parseInt(window.localStorage["last_message_id"])!=0){
					pullCahts();
				}else{
					window.localStorage.setItem("last_message_id",json["private_mail"].message_id);
					window.localStorage.setItem("sendData",JSON.stringify(json));
					uexWindow.evaluatePopoverScript("privateLetter","content","appendSMS()");
				}
			}else{
				
			}
		}
	};
	uexXmlHttpMgr.open(httpID,'post',sendUrl,3000);
	uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"uid",window.localStorage["chatUid"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"content",content);
	uexXmlHttpMgr.send(httpID);
}

function  appendSMS(){
	var json=window.localStorage["sendData"];
	json=eval("("+json+")");
	var obj=json["private_mail"];
	obj["ctime"]=addFriendlyTime2(obj.ctime);
	obj["userHead"]=obj["user"].avatar_url;
	var my_message=document.createElement("DIV");
	var message=zy_tmpl_s(tmpl_right,obj,zy_tmpl_count(obj));
	my_message.innerHTML=message;
	document.body.appendChild(my_message);
	var content=$$("bodyContent");
	content.appendChild(my_message);
	window.scrollTo(1000000,1000000);
	
}
function selectFriend(){
	

	var httpID=753;
	uexXmlHttpMgr.onData=function (inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				var len=zy_tmpl_count(json["list"]);
				if(parseInt(window.localStorage["friendOffset"])!=0&&len!=0){
					var btn_add=$$("Friend");
					btn_add.parentNode.removeChild(btn_add);
				}else if(len==0){
					uexWindow.toast(1,5,"没有更多",1000);
				}
				window.localStorage.setItem("friendOffset",parseInt(window.localStorage["friendOffset"])+len);
				for(var i=0;i<len;i++){
					var obj=json["list"][i];
					var d=document.createElement("DIV");
					var friend=zy_tmpl_s(tmpl_head,obj,zy_tmpl_count(obj));
					d.innerHTML=friend;
					document.body.appendChild(d);
				}
				if(len!=0){
					var add=document.createElement("DIV");
					var end=zy_tmpl_s(tmpl_add_Fol,obj,zy_tmpl_count(obj));
					add.innerHTML=end;
					document.body.appendChild(add);
				}
			}else{
				uexWindow.toast(1,5,json["message"],500);
			}
		}
	}
	uexXmlHttpMgr.open(httpID,'post',friendUrl,3000);
	uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"uid",'1');
	uexXmlHttpMgr.setPostData(httpID,'0',"offset",window.localStorage["friendOffset"])
	uexXmlHttpMgr.send(httpID);
}
function chat(obj,uid){
	var nickname=obj.id;
	window.localStorage.setItem("chatUid",uid);
	window.localStorage.setItem("chatNickName",nickname);
	window.localStorage.setItem("is_pullHistory",false);
	uexWindow.open("privateLetter",0,"privateLetter.html",4,"","",0,500);
}

/**
 * @argument 拉取最近联系人
 */
function pullRecentContact(){
	var httpID=57489;
	window.localStorage.setItem("is_pullHistory",true);
	uexXmlHttpMgr.onData=function (inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				window.localStorage.setItem("last_message_id",json["last_message_id"]);
				window.localStorage.setItem("recentContactData",JSON.stringify(json));
				var len=zy_tmpl_count(json["list"]);
				if(len!=0){
					window.localStorage.setItem("recentContactOffset",(len+parseInt(window.localStorage["recentContactOffset"])));
					for(var i=0;i<len;i++){
						var obj=json["list"][i];
						obj["ctime"]=addFriendlyTime2(obj["last_contact_time"]);
						obj["nickname"]=obj["user"].nickname;
						obj["headPic"]=obj["user"].avatar_url;
						obj["uid"]=obj["user"].uid;
						var div=document.createElement("DIV");
						var recentUser=zy_tmpl_s(tmpl_recentContact,obj,zy_tmpl_count(obj));
						div.innerHTML=recentUser;
						document.body.appendChild(div);
					}
					var div=document.createElement("DIV");
					var add=zy_tmpl_s(tmpl_add_Rec,json,zy_tmpl_count(obj));
					div.innerHTML=add;
					document.body.appendChild(div);
					
				}else{
					uexWindow.toast(1,5,"没有更多了",1000);
				}
			}else{
				uexWindow.toast(1,5,json["message"],1000);
			}
		}
	}
	uexXmlHttpMgr.open(httpID,'post',pullRecentContactUrl,3000);
	uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
	var lens=parseInt(window.localStorage["recentContactOffset"]);

	if(parseInt(window.localStorage["recentContactOffset"])!=0){
		uexXmlHttpMgr.setPostData(httpID,'0',"offset",window.localStorage["recentContactOffset"]);
	}
	uexXmlHttpMgr.send(httpID);
}

function enterChat(obj){
	var uid=obj.id;
	var last_message_id=$('#'+uid).attr('name');
	var nickname=$('#'+uid).attr('value');
	window.localStorage.setItem("is_pullHistory",true);
	window.localStorage.setItem("last_message_id",last_message_id);
	window.localStorage.setItem("chatUid",uid);
	window.localStorage.setItem("chatNickName",nickname);
	uexWindow.open("privateLetter",0,"privateLetter.html",4,"","",0,500);
}

function pullHistoryChatData(){
	var httpID=738957;
	window.localStorage.setItem("is_history",true);
	uexXmlHttpMgr.onData=function (inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				window.localStorage.setItem("pullData",JSON.stringify(json));
				pullDataAppend();
			}else{
				uexWindow.toast(1,5,json["message"],1000);
			}
		}
	}
	uexXmlHttpMgr.open(httpID,'post',pullPrevMessageUrl,3000);
	uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"uid",window.localStorage["chatUid"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"first_message_id",window.localStorage["last_message_id"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"count",20);
	uexXmlHttpMgr.send(httpID);
	
}


